'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: mcp_search_handle.py
* @Time: 2025/11/21
* @All Rights Reserve By Brtc
'''
from mcp_core.mcp_client import MCPClient
from mcp_core.mcp_protocol import StatusCode


def example_search_operation():
    print("Example Of Search Operation!!")
    with MCPClient(host="127.0.0.1", port=8080) as client:
        print("Created Mult Context")
        contexts = []
        for i in range(10):
            response = client.create_context(
                content = {
                    "id":i,
                    "title":f"文档{i}",
                    "description":f"这是第{i}个文档！",
                },
                metadata={
                    "labels":["doc", f"category_{i%3}"],
                    "scope":"task"
                }
            )
            if response.status_code == StatusCode.CREATED.value:
                contexts.append(response.data["context"]["id"])

        print(f"总共创建了{len(contexts)}个上下文！")

        #按关键词搜索所
        print("按照关键词搜索！")
        response = client.search_contexts(keyword="文档0")
        print(f"状态码: {response.status_code}")
        print(f"找到{response.data['count']}个匹配的上下文！")
        for result in response.data['results']:
            print(f"------------------: {result['id']}: {result['content']['title']}")

        #按照lable搜索
        print("按照标签搜索！")
        response = client.search_contexts(labels=["category_0"])
        print(f"状态码: {response.status_code}")
        print(f"找到{response.data['count']}个匹配的上下文！")
        for result in response.data['results']:
            print(f"------------------: {result['id']}: {result['content']['title']}")


if __name__ == '__main__':
    example_search_operation()